﻿/**
 * Copyright (c) 2011-2016, Mobangjack 莫帮杰 (mobangjack@foxmail.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace util
{
    class SequencedList<T> : IList<T>
    {
        private T[] array;
        private int count;

        public SequencedList(int initialCapacity)
        {
            array = new T[initialCapacity];
            count = 0;
        }

        public void Add(T item)
        {
            Arr<T>.DoubleSizeOnThreshold(array, count);
            array[count++] = item;
        }

        public void Insert(int index, T item)
        {
            if (index > array.Length - 1)
            {
                throw new ArgumentOutOfRangeException("index out of range");
            }
            if (index < count)
            {
                if (count > array.Length)
                {
                    T[] tmp = new T[2 * array.Length];
                    for (int i = 0; i < index; i++)
                    {
                        tmp[i] = array[i];
                    }
                    tmp[index] = item;
                    for (int i = index; i < count; i++)
                    {
                        tmp[i + 1] = array[i];
                    }
                    array = tmp;
                }
                else
                {
                    for (int i = count - 1; i > index; i--)
                    {
                        array[i] = array[i - 1];
                    }
                    array[index] = item;
                }
            }
            else
            {
                array[index] = item;
            }
            /*
            if(index < count)
            {
                if (count > array.Length)
                {
                    T[] tmp = new T[2 * array.Length];
                    for (int i = 0; i < index; i++)
                    {
                        tmp[i] = array[i];
                    }
                    tmp[index] = item;
                    for(int i = index; i < count; i++)
                    {
                        tmp[i + 1] = array[i];
                    }
                    array = tmp;
                }
                else
                {
                    for (int i = count - 1; i > index; i--)
                    {
                        array[i] = array[i-1];
                    }
                    array[index] = item;
                }
            }
            else if(index < array.Length)
            {
                array[index] = item;
            }
            else if(index > array.Length - 1)
            {
                T[] tmp = new T[index * 2];
                for (int i = 0; i < count; i++)
                {
                    tmp[i] = array[i];
                }
                tmp[index] = item;
                for (int i = index + 1; i < count; i++)
                {
                    tmp[i] = array[i];
                }
                array = tmp;
            }
             * */
            count++;
        }

        public T Remove(int index)
        {
            if(index >= count)
            {
                throw new ArgumentOutOfRangeException();
            }
            T tmp = array[index];
            for (int i = index; i < count - 1; i++)
            {
                array[i] = array[i + 1];
            }
            count--;
            return tmp;
        }

        public int Remove(T item)
        {
            int index = IndexOf(item);
            if(index > -1)
            {
                Remove(index);
            }
            return index;
        }

        public T Replace(int index, T item)
        {
            if (index >= count)
            {
                throw new ArgumentOutOfRangeException();
            }
            T tmp = array[index];
            array[index] = item;
            return tmp;
        }

        public int Replace(T oldItem, T newItem)
        {
            int index = IndexOf(oldItem);
            if(index > -1)
            {
                Replace(index, newItem);
            }
            return index;
        }

        public int Capacity()
        {
            return array.Length;
        }

        public int Count()
        {
            return count;
        }

        public T[] ToArray()
        {
            T[] tmp = new T[count];
            for (int i = 0; i < count; i++)
            {
                tmp[i] = array[i];
            }
            return tmp;
        }

        public int IndexOf(T item)
        {
            for (int i = 0; i < count; i++)
            {
                if (EqualityComparer<T>.Default.Equals(item, array[i]))
                {
                    return i;
                }
            }
            return -1;
        }

        public T Get(int index)
        {
            return array[index];
        }

    }
}
